define(['jquery', 'bootstrap', 'backend', 'table', 'form', '/assets/addons/kuyou/layui/layui.js'], function ($, undefined, Backend, Table, Form, Layui) {

    var Controller = {
        index: function () {

            layui.use(['table','upload','form'], function(){
                var table = layui.table;
                upload = layui.upload;

                table.render({
                    elem: '#table'
                    ,url:'kuyou/invoicing/index'
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.rows //解析数据列表
                         };
                    }
                    ,toolbar: '#thisToolbar' //开启头部工具栏，并为其绑定左侧模板
                    ,page: true
                    ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,cols: [[
                        {checkbox: true},
                        {field: 'suppliers_code', title: __('Suppliers_code'), operate: 'LIKE'},
                        {field: 'suppliers.supplyer_name', title: __('Suppliers.supplyer_name'),templet: function (d) {
                                return d.suppliers.supplyer_name;
                            }},
                        {field: 'bill_codes', title: __('Bill_codes')},
                        {field: 'money', title: __('Money')},
                        {field: 'tax', title: __('Tax')},
                        {field: 'real_money', title: __('Real_money')},
                        {field: 'status', title: __('Status'),templet: function (d) {
                                if(d.status==0){
                                    return __('Status 0');
                                }
                                if(d.status==1){
                                    return __('Status 1');
                                }
                                if(d.status==2){
                                    return __('Status 2');
                                }
                                if(d.status==3){
                                    return __('Status 3');
                                }
                                if(d.status==4){
                                    return __('Status 4');
                                }
                                if(d.status==5){
                                    return __('Status 5');
                                }
                            }},
                        {field: 'times', title: __(__('Invoice date')),templet: function (d) {
                                return layui.util.toDateString(d.times*1000,'yyyy-MM-dd');
                            }},
                        {field: 'pay_money', title: __(__('Invoice amount'))},
                        {field: 'invoice', title: __(__('Invoice number'))},
                        {field: 'invoice_header', title: __(__('Invoice header'))},
                        {fixed: false, title:__('Operate'), toolbar: '#delDemo', width:100}
                    ]]
                });

                //头工具栏事件
                table.on('toolbar(table)', function(obj){
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch(obj.event){
                        case 'refresh':
                            table.reload('table', []);
                            break;
                        case 'add':
                            Fast.api.open("kuyou/invoicing/add", __('Invoice issuance'), {
                               callback:function(value){
                                  Controller.api.bindevent();
                               },
                                area:['90%','75%'],
                            });
                        break;
                        case 'delAll':
                            var data = checkStatus.data;
                            var ids = '';
                            $(data).each(function (k,v) {
                                ids+=v.id+',';
                            });
                            if(data.length<=0){
                                Toastr.error(__('Please select the data to be deleted'));return;
                            }
                            Layer.confirm(
                                __('Are you sure you want to delete the %s selected item?', data.length),
                                {icon: 3, title: __('Warning'), shadeClose: true},
                                function (index) {
                                    Fast.api.ajax("kuyou/invoicing/del?ids="+ids, function () {
                                        reloadTable()
                                    },'',true);
                                    Layer.close(index);
                                }
                            );
                        break;
                    }
                });
                function reloadTable(){
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                }
                //监听行工具事件
                table.on('tool(table)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'del'){
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Are you sure you want to delete this item?'),
                            {icon: 3, title: __('Warning'),offset: [top, left], shadeClose: true},
                            function (index) {
                                $.ajax( {
                                    url: "kuyou/invoicing/del",
                                    type: "POST",
                                    data:{'ids':data.id},
                                    success: function(res){
                                        if(res.code==1){
                                            Toastr.success(__('Operation successful'));
                                            setTimeout(function () {
                                                reloadTable();
                                            },1000)
                                        }else{
                                            Toastr.error(res.msg);
                                        }
                                    }
                                });
                                Layer.close(index);
                            }
                        );
                    } else if(obj.event === 'edit'){
                        Fast.api.open("kuyou/invoicing/edit?ids="+data.id, __('edit'));
                    }
                });
                //搜索事件
                $('#search').on('click', function () {
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                });
                function getSearch() {
                    var search = {};
                    var suppliers_code = $('#suppliers_code').val();
                    if(suppliers_code!=''){
                        search.suppliers_code = suppliers_code;
                    }
                    var bill_codes = $('#bill_codes').val();
                    if(bill_codes!=''){
                        search.bill_codes = bill_codes;
                    }
                    var bill_time = $('#bill_time').val();
                    if(bill_time!=''){
                        search.bill_time = bill_time;
                    }
                    return search;
                }
                function getOp() {
                    var op = {};
                    op.bill_codes = 'LIKE';
                    op.suppliers_code = 'LIKE';
                    op.bill_time = 'RANGE';
                    return op;
                }
            });
            Controller.api.bindevent();
        },
        
        add: function () {
            layui.use(['table','upload','form'], function(){
                var table = layui.table;
                upload = layui.upload;

                table.render({
                    elem: '#invoicingTable'
                    ,url:'kuyou/invoicing/list'
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.rows //解析数据列表
                        };
                    }
                    ,toolbar: '#thisToolbar' //开启头部工具栏，并为其绑定左侧模板
                    ,page: true
                    ,defaultToolbar:[]
                    ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,cols: [[
                        {checkbox: true},
                        {field: 'bill_time', title: __(__('Document date')),templet: function (d) {
                                return layui.util.toDateString(d.bill_time*1000,'yyyy-MM-dd');
                            }},
                        {field: 'bill_codes', title: __(__('Document Number')), width: '160'},
                        {field: 'suppliers.supplyer_name', title: __(__('supplier')),templet: function (d) {
                                return d.suppliers.supplyer_name;
                            }, width: '160'},
                        {field: 'money', title: __(__('amount of money'))},
                        {field: 'tax', title: __(__('Tax amount'))},
                        {field: 'status', title: __(__('Payment status')),templet: function (d) {
                                if(d.status==0){
                                    return __('Status 0');
                                }
                                if(d.status==1){
                                    return __('Status 1');
                                }
                                if(d.status==2){
                                    return __('Status 2');
                                }
                                if(d.status==3){
                                    return __('Status 3');
                                }
                                if(d.status==4){
                                    return __('Status 4');
                                }
                                if(d.status==5){
                                    return __('Status 5');
                                }
                            }},
                        {field: 'real_money', title: __(__('Total price and tax'))},
                        {field: 'pay_money', title: __(__('Invoiced amount'))},
                        {field: 'give_money', title: __(__('Unlicensed amount'))},
                        {field: 'invoicing', title: __(__('The invoiced amount this time')),edit:'number', width: '140'},
                    ]]
                });

                //头工具栏事件
                table.on('toolbar(table)', function(obj){
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    var ids = '';
                    $(data).each(function (k,v) {
                        ids+=v.id+',';
                    });
                    switch(obj.event){
                        case 'refresh':
                            table.reload('table', []);
                            break;
                        case 'add':
                            if(ids==''){
                                Toastr.error(__('Please select the data to be invoiced'));return false;
                            }
                            var invoice = $('#c-invoice').val();
                            if(invoice==''){
                                Toastr.error(__('Please enter invoice number'));return false;
                            }
                            var invoice_header = $('#c-invoice_header').val();
                            if(invoice_header==''){
                                Toastr.error(__('Please enter the invoice header'));return false;
                            }
                            var arr = new Array();
                            $(data).each(function (k,v) {
                                if(v.invoicing==''){
                                    Toastr.error(__('Please enter the invoice amount'));return false;
                                }
                                var row = new Array();
                                row['id'] = v.id;
                                row['invoicing'] = v.invoicing;
                                arr.push(row);
                            });

                            $.ajax( {
                                url: "kuyou/invoicing/add",
                                type: "POST",
                                data:{'row':arrayToJsonString(arr),'invoice':invoice,'invoice_header':invoice_header},
                                success: function(res){
                                    if(res.code==0){
                                        Toastr.error(res.msg);
                                    }else{
                                        Toastr.success(__('Operation successful'));
                                        setTimeout(function () {
                                            parent.location.reload();
                                        },1000)
                                    }
                                }
                            });
                            break;
                    }
                });

                function arrayToJsonString(o) {
                    var len = o.length;
                    var new_arr = new Array();
                    var str = '',strone='',strs='',jsonstr='';
                    for(var i = 0;i<len;i++){
                        new_arr = o[i];
                        for(var k in new_arr){
                            strone += '"'+k+'"'+':'+'"'+new_arr[k]+'"'+',';
                        }
                        str = '{'+strone.substring(0,strone.length-1)+'}';
                        strone='';
                        strs += str+',';
                        new_arr=[];
                    }
                    strs = '['+strs.substring(0,strs.length-1)+']';
                    return strs;
                }

                //搜索事件
                $('#search').on('click', function () {
                    table.reload('invoicingTable', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                });
                function getSearch() {
                    var search = {};
                    var suppliers_code = $('#suppliers_code').val();
                    if(suppliers_code!=''){
                        search.suppliers_code = suppliers_code;
                    }
                    var bill_codes = $('#bill_codes').val();
                    if(bill_codes!=''){
                        search.bill_codes = bill_codes;
                    }
                    var bill_time = $('#bill_time').val();
                    if(bill_time!=''){
                        search.bill_time = bill_time;
                    }
                    return search;
                }
                function getOp() {
                    var op = {};
                    op.bill_codes = 'LIKE';
                    op.suppliers_code = 'LIKE';
                    op.bill_time = 'RANGE';
                    return op;
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                layui.use(['table','form'], function(){
                    var supplyer_div = xmSelect.render({
                        language: Config.language,
                        el: '#supplyer_codes',
                        filterable: true,
                        tips: __('Please select a supplier'),
                        paging: true,
                        pageSize: 5,
                        // remoteSearch: true,
                        pageRemote: true,
                        remoteMethod: function (val, cb, show, pageIndex) {
                            $.ajax({
                                url: "kuyou/suppliers/index",
                                type: 'GET',
                                crossOrigin: true,
                                dataType: 'json',
                                cache: false,
                                async: true,
                                data: {
                                    keywords: val,
                                    filter:JSON.stringify({"supplyer_name":val}),
                                    op:JSON.stringify({"supplyer_name":'like'}),
                                    page: pageIndex,
                                    limit: 5
                                },
                                success: function (data) {
                                    var TotalCount = data.total;
                                    var Pages = Math.ceil(TotalCount/5);
                                    var list = [];
                                    $(data.rows).each(function (k,v) {
                                        var row = [];
                                        row['name'] = v.supplyer_name;
                                        row['value'] = v.supplyer_code;
                                        list.push(row)
                                    });
                                    setTimeout(function () {
                                        cb(list, Pages)
                                    }, 100)
                                },
                                error: function (data) {
                                    cb([], 0);
                                }
                            });
                        },
                        on: function(data){
                            var arr = data.arr;
                            var change = data.change;
                            var isAdd = data.isAdd;
                            if(!isAdd){
                                $('#suppliers_code').val('');
                            }
                            if(change.length>0){
                                for (let i in arr){
                                    if(change[0].parentId==arr[i].parentId){
                                        arr.splice(i,1,change[0]);
                                    }
                                }
                            }
                            if(arr.length>0){
                                if(arr[0]['value']!=undefined){
                                    $('#suppliers_code').val(arr[0]['value']);
                                }
                            }
                            return Array.from(new Set(arr));
                        },
                    });

                    var kuyou_suppliers = Config.kuyou_suppliers;
                    if(kuyou_suppliers!=undefined){
                        var newArr = [];
                        newArr.push({name: kuyou_suppliers.supplyer_name, value: kuyou_suppliers.supplyer_code,});
                        supplyer_div.setValue(newArr);
                    }

                    var set = Config.set;
                    var examine = parseInt(set.examine.is_checked);
                    if(isNaN(examine)){
                        examine = 0;
                    }
                    if(examine==1 && Config.examine_status==1){
                        supplyer_div.update({disabled:true})
                    }
                });

                layui.use('laydate', function(){
                    var laydate = layui.laydate;
                    //执行一个laydate实例
                    
                    laydate.render({
                            lang: Config.language,
                        elem: '#c-bill_time',
                        type: 'datetime'
                    });
                    laydate.render({
                            lang: Config.language,
                        elem: '#c-purchase_time',
                    });
                    laydate.render({
                            lang: Config.language,
                         elem: '#bill_time',
                         type: 'datetime',
                        lang: Config.language,
                         range: true
                    });
                });
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    parent.location.reload();
                });
            }
        }
    };
    return Controller;
});